package main

import (
	"context"

	"github.com/go-redis/redis/v8"
)

var ctx = context.Background()

func NewRedisClient() *redis.Client {
	client := redis.NewClient(&redis.Options{
		Addr:     RedisAddress,
		Password: RedisPassword,
		DB:       0,
	})

	_, err := client.Ping(ctx).Result()
	if err != nil {
		panic(err)
	}

	return client
}

func PushMessage(client *redis.Client, queueName string, message string) error {
	return client.RPush(ctx, queueName, message).Err()
}

func PullMessages(client *redis.Client, queueName string, count int64) ([]string, error) {
	return client.LRange(ctx, queueName, 0, count-1).Result()
}

func AckMessages(client *redis.Client, queueName string, count int64) error {
	return client.LTrim(ctx, queueName, count, -1).Err()
}
